from django.contrib import admin
from .models import Material, Device, WorkOrder, QualityCheck


@admin.register(Material)
class MaterialAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'specification', 'unit', 'price', 'is_active', 'created_at']
    list_filter = ['is_active', 'unit', 'created_at']
    search_fields = ['name', 'code', 'specification']
    list_editable = ['price', 'is_active']
    readonly_fields = ['created_at']
    fieldsets = [
        ('基本信息', {
            'fields': ['name', 'code', 'specification', 'unit']
        }),
        ('价格信息', {
            'fields': ['price']
        }),
        ('状态信息', {
            'fields': ['is_active', 'created_at']
        })
    ]


@admin.register(Device)
class DeviceAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'location', 'status', 'install_date', 'last_maintenance', 'is_active']
    list_filter = ['status', 'is_active', 'install_date', 'last_maintenance']
    search_fields = ['name', 'code', 'location']
    list_editable = ['status', 'is_active']
    readonly_fields = ['created_at']
    fieldsets = [
        ('基本信息', {
            'fields': ['name', 'code', 'location']
        }),
        ('状态信息', {
            'fields': ['status', 'is_active']
        }),
        ('时间信息', {
            'fields': ['install_date', 'last_maintenance', 'created_at']
        })
    ]


@admin.register(WorkOrder)
class WorkOrderAdmin(admin.ModelAdmin):
    list_display = ['order_no', 'material', 'device', 'quantity', 'status', 'start_time', 'end_time']
    list_filter = ['status', 'material', 'device', 'created_at']
    search_fields = ['order_no', 'material__name', 'device__name']
    list_editable = ['status']
    readonly_fields = ['created_at']
    raw_id_fields = ['material', 'device']
    fieldsets = [
        ('工单信息', {
            'fields': ['order_no', 'material', 'device', 'quantity']
        }),
        ('状态信息', {
            'fields': ['status', 'start_time', 'end_time']
        }),
        ('备注信息', {
            'fields': ['notes', 'created_at']
        })
    ]


@admin.register(QualityCheck)
class QualityCheckAdmin(admin.ModelAdmin):
    list_display = ['work_order', 'check_item', 'result', 'check_value', 'standard_value', 'checker', 'check_time']
    list_filter = ['result', 'checker', 'check_time', 'work_order__status']
    search_fields = ['work_order__order_no', 'check_item', 'checker']
    list_editable = ['result']
    readonly_fields = ['check_time']
    raw_id_fields = ['work_order']
    fieldsets = [
        ('检验信息', {
            'fields': ['work_order', 'check_item', 'result']
        }),
        ('检验数据', {
            'fields': ['check_value', 'standard_value']
        }),
        ('检验记录', {
            'fields': ['checker', 'check_time', 'notes']
        })
    ]




# 自定义Admin站点标题
admin.site.site_header = 'MES制造执行系统'
admin.site.site_title = 'MES管理系统'
admin.site.index_title = '欢迎使用MES制造执行系统' 